#import "drawundo.h"

@interface GroupGraphicsChange(PrivateMethods)

- undoDetails;
- redoDetails;

@end

@implementation GroupGraphicsChange

- free
{
    if (![self hasBeenDone]) {
        [[group subGraphics] empty];
        [group free];
    }
    return [super free];
}

- (const char *)changeName
{
    return NXLocalStringFromTable("Operations", "Group", NULL, "The operation of grouping a bunch of graphical entities together.");
}

- saveBeforeChange
{
    [super saveBeforeChange];
    [changeDetails makeObjectsPerform:@selector(recordGraphicPositionIn:) with:[graphicView graphics]];

    return self;
}

- changeDetailClass
{
    return [OrderChangeDetail class];
}

- noteGroup:aGroup
{
    group = aGroup;
    return self;
}

- undoDetails
{
    int count, i;
    id detail, graphic;
    List *allGraphics;

    allGraphics = [graphicView graphics];
    [allGraphics removeObject:group];
    count = [changeDetails count];
    for (i = 0; i < count; i++) {
	detail = [changeDetails objectAt:i];
	graphic = [detail graphic];
	[graphic setCacheable:YES];
        [allGraphics insertObject:graphic at:[detail graphicPosition]];
    }
    [graphicView getSelection];
    [graphicView resetGroupInSlist];
	
    return self;
}

- redoDetails
{
    int count, i;
    id detail, graphic;
    List *selectedGraphics;
    List *allGraphics;

    selectedGraphics = [graphicView selectedGraphics];
    allGraphics = [graphicView graphics];
    count = [changeDetails count];
    for (i = 0; i < count; i++) {
	detail = [changeDetails objectAt:i];
	graphic = [detail graphic];
	[graphic setCacheable:NO];
        [selectedGraphics removeObject:graphic];
        [allGraphics removeObject:graphic];
    }
    [allGraphics insertObject:group at:0];
    [graphicView setGroupInSlist:YES];
    [graphicView getSelection];


    return self;
}

@end